# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>

pkgname=libphonenumber
pkgver=8.13.25
pkgrel=1
epoch=1
pkgdesc="Google's common library for parsing, formatting, and validating international phone numbers"
url="https://github.com/googlei18n/libphonenumber"
arch=(loong64 x86_64)
license=(Apache)
depends=(
  abseil-cpp
  icu
  protobuf
)
makedepends=(
  cmake
  git
  gtest
  jre-openjdk-headless
)
_commit=de1e702ba39f9e5a21e44c5c30b895dfab4d5eb6  # tags/v8.13.25^0
source=(
  "git+$url#commit=$_commit"
  protobuf-targets.patch
)
b2sums=('SKIP'
        '911187934b234eeda64e64513daa2ad7ce5bbb56e91921cad49334286c147acafdaf7343545b3f896ab26ead774902b9f17ace93c8a49ae4b0e9763c0009781e')

pkgver() {
  cd $pkgname
  git describe --tags | sed 's/^libphonenumber-\|^v//;s/[^-]*-g/r&/;s/-/+/g'
}

prepare() {
  cd $pkgname

  # Use official protobuf cmake target instead of
  # custom one, fixes build with protobuf 23
  git apply -3 ../protobuf-targets.patch

  # Fix build with abseil-cpp 2023
  sed -e 's|CMAKE_CXX_STANDARD 11|CMAKE_CXX_STANDARD 17|' \
      -i tools/cpp/CMakeLists.txt
}

build() {
  local cmake_options=(
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCMAKE_BUILD_TYPE=None
    -DCMAKE_CXX_STANDARD=17
    -DUSE_BOOST=OFF
    -DUSE_STDMUTEX=ON
  )

  # Greatly reduce size of libgeocoding's relocation tables
  # https://gitlab.archlinux.org/archlinux/rfcs/-/blob/master/rfcs/0023-pack-relative-relocs.rst
  LDFLAGS+=" -Wl,-z,pack-relative-relocs"

  cmake -S $pkgname/cpp -B build "${cmake_options[@]}"
  cmake --build build
}

check() {
  cmake --build build --target tests
}

package() {
  depends+=(
    libicu{uc,i18n}.so
    libprotobuf.so
  )
  provides+=(
    libgeocoding.so
    libphonenumber.so
  )

  DESTDIR="$pkgdir" cmake --install build
}

# vim:set sw=2 sts=-1 et:
